﻿function markFeedRead() {
    feedFieldSet = $(this).parents('fieldset');
    if (!feedFieldSet.hasClass('read')) {
        $.post(
            $('#mark-read-feed-item-url').val(),
            {
                feedItemId: feedFieldSet.find('>#feedItemId').val()
            },
            function(data, textStatus, XMLHttpRequest){
                if (XMLHttpRequest.status == 200) {
                    feedFieldSet.addClass('read');
                }
            }
        );
    }
}

var sorting = null;
var filter = null;
var feedId = null;

function updateFeedItems()
{
    var loadUrl = $("#feed-items-get-list-url").val();
    $.get(
        loadUrl,
        {
            feedId: feedId,
            filter: filter,
            sorting: sorting
        },
        function(data, statusText, XMLHttpRequest) {
            if (XMLHttpRequest.status == 200) {
                $('#right-content').html(data);
                $('.star').rating({
                    cancelValue: '0',
                    callback: function(value, form) {
                        $(form).ajaxSubmit();
                    }
                });
                $('.star.disabled').rating('readOnly', true);
            }
        }
    );
}
$(function() {
    $("#subscriptions").tree(
        {
            data: {
                async: true,
                type: 'json',
                opts: {
                    method: 'GET',
                    url: $('#subscriptions-get-tree-url').val()
                }
            },
            callback: {
                onselect: function(node, tree) {
                    feedId = tree.get(node).attributes.id;
                    updateFeedItems();
                }
            },
            ui: {
                theme_name: "classic",
                dots: false
            },
            type: {
                clickable: true
            }
        }
    );

    $(".feed-items .feed-item-content").live("click", markFeedRead);

    $("#feed-tools select#feed-item-filter").live("change", function() {
        filter = $(this).val();
        updateFeedItems()
    });

    $("#feed-tools select#feed-item-sorting").live("change", function() {
        sorting = $(this).val();
        updateFeedItems();
    });

    $('#add-folder-dialog form').validate({
        submitHandler: function(form) {
            $(form).ajaxSubmit({
                dataType: 'json',
                success: function(responseData, statusText, xhr) {
                    if (xhr.status == 200) {
                        var tree = $.tree.reference("subscriptions");
                        var node = tree.create(
                            {
                                data: $('#add-folder-dialog form #Name').val(),
                                attributes: { type: 'folder', id: responseData.id }
                            },
                            -1
                        );
                    }
                }
            });
            $('#add-folder-submit').parents().dialog('close');
        }
    });

    $('#add-folder-button').live('click', function() {
        $('#add-folder-dialog form').resetForm();
        $('#add-folder-dialog').dialog({
            modal: true,
            resizable: false,
            title: 'New folder',
            width: 'auto',
            height: 'auto',
            minHeight: 0,
            open: function() {
                $('#add-folder-dialog form #Name').focus();
            }
        });
    });
    $('#add-feed-button').live('click', function() {
        $('#add-feed-dialog').resetForm();
        $('#add-feed-dialog').dialog({
            modal: true,
            resizable: false,
            title: 'New feed',
            width: 'auto',
            height: 'auto',
            minHeight: 0,
            open: function() {
                var tree = $.tree.reference("subscriptions");
                var foldersOptions = '';
                tree.children(-1).each(function() {
                    foldersOptions = foldersOptions + '<option value="' + tree.get(this).attributes.id + '"'
                    if (tree.selected && tree.get(tree.selected).attributes.id == tree.get(this).attributes.id) {
                        foldersOptions += ' selected="selected"';
                    }
                    foldersOptions = foldersOptions + '>' + tree.get(this).data.title + '</option>';
                });
                $('#add-feed-dialog #FolderId').html(foldersOptions);
                $('#add-feed-dialog #Url').focus();
            }
        });
    });
});